<cfcomponent extends="AbsDAO" output="false">

	<cffunction name="create" access="public" returntype="Numeric" output="false">
		<cfargument name="role" type="it.millemultimedia.easyBlog.model.Role" required="true" />	
		<cfset var qry = "" />	
		<cfset var id = 0 />
		
		<cftransaction>
		
			<cftry>
            	<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
					INSERT INTO Role
					(
						name,description
					)
					VALUES
					(
						<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.role.getname()#" null="false" />,
					<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.role.getdescription()#" null="false" />
					)
				</cfquery>
				
				<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
				SELECT MAX(idrole) AS id
				FROM Role
				</cfquery>
				
				<cfset id = qry.id />
				
				<cfset arguments.role.setIdRole( id )/>
				
				<cfset setPreferences( arguments.role )/>
				<cfset setPermissions( arguments.role )/>  
				          
	            <cfcatch type="Any" >
	            
	            	<cftransaction action="rollback" />
					
					<cfthrow message="#cfcatch.Message#" detail="#cfcatch.Detail#" />
	            
	            </cfcatch>
			
            </cftry>

		</cftransaction>

		<cfreturn id />
	</cffunction>
	
	<cffunction name="read" access="public" returntype="Query" output="false">
		<cfargument name="role" type="it.millemultimedia.easyBlog.model.Role" required="true" />
		<cfset var qry="" />
		
		<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
			SELECT idrole,name,description
			FROM Role
			WHERE idrole = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.role.getidRole()#" null="false" />
		</cfquery>
		
		<cfreturn qry />
	</cffunction>
	
	<cffunction name="update" access="public" returntype="void" output="false">
		<cfargument name="role" type="it.millemultimedia.easyBlog.model.Role" required="true" />
		
		<cfset var qry="" />
		
		<cftransaction>
		
			<cftry>
			
	        	<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
					UPDATE Role
					SET name = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.role.getname()#" null="false" />,
						description = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.role.getdescription()#" null="false" />
					WHERE idrole = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.role.getidRole()#" null="false" />
				</cfquery>
				
				<cfset setPreferences( arguments.role )/>
				<cfset setPermissions( arguments.role )/>  
				      
		        <cfcatch type="Any" >
		        
					<cftransaction action="rollback" />
						
					<cfthrow message="#cfcatch.Message#" detail="#cfcatch.Detail#" />
				
		        </cfcatch>
				
	        </cftry>

		</cftransaction>

	</cffunction>
	
	<cffunction name="delete" access="public" returntype="void" output="false">
		<cfargument name="role" type="it.millemultimedia.easyBlog.model.Role" required="true" />
		
		<cfset var qry="" />

		<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
			DELETE FROM Role
			WHERE idrole = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.role.getidRole()#" null="false" />
		</cfquery>
	</cffunction>
	
	
	<cffunction name="list" access="public" returntype="Query" output="false">
		<cfargument name="role" type="it.millemultimedia.easyBlog.model.Role" required="true" />
		
		<cfset var qry="" />

		<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
			SELECT * 
			FROM Role
			WHERE idRole is NOT NULL 
			<cfif !isNull( arguments.role.getidRole() ) >
			AND  idRole = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.role.getidRole()#" null="false" />
			</cfif>
			
				
			<cfif !isNull( arguments.role.getname() ) >
			AND  name = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.role.getname()#" null="false" />
			</cfif>
			
				
			<cfif !isNull( arguments.role.getdescription() ) >
			AND  description = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.role.getdescription()#" null="false" />
			</cfif>
			
		</cfquery>
		
		<cfreturn qry/>
	</cffunction>
	
	<cffunction name="setPreferences" access="private" returntype="void">
		<cfargument name="role" type="it.millemultimedia.easyBlog.model.Role" required="true" />
		
		<cfset var qry = "" />
		<cfset var next = "" />
		<cfset var iterator = arguments.role.getPreferences().iterator() />
		
		<cftransaction>
		
			<cftry>
			
				<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
					DELETE Role_Preference
					WHERE idRole = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.role.getIdRole()#" null="false" />
				</cfquery>
				
				<cfloop condition="#iterator.hasNext()#">
				
					<cfset next = iterator.next() />
			
					<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
						INSERT INTO Role_Preference
						(
							idRole,idPreference
						)
						VALUES
						(
						<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.role.getIdRole()#" null="false" />,
						<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#next.getIdPreference()#" null="false" />
						)
					</cfquery>
			
				</cfloop>
				
				<cfcatch type="any">
				
					<cftransaction action="rollback" />
					
					<cfthrow message="#cfcatch.Message#" detail="#cfcatch.Detail#" />
				
				</cfcatch>
			
			</cftry>
			
		</cftransaction>
			
	</cffunction>
	
	<cffunction name="setPermissions" access="private" returntype="void">
		<cfargument name="role" type="it.millemultimedia.easyBlog.model.Role" required="true" />
		
		<cfset var qry = "" />
		<cfset var next = "" />
		<cfset var iterator = arguments.role.getPermissions().iterator() />
		
		<cftransaction>
		
			<cftry>
			
				<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
					DELETE Role_Permission
					WHERE idRole = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.role.getIdRole()#" null="false" />
				</cfquery>
				
				<cfloop condition="#iterator.hasNext()#">
				
					<cfset next = iterator.next() />
			
					<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
						INSERT INTO Role_Permission
						(
							idRole,idPermission
						)
						VALUES
						(
						<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.role.getIdRole()#" null="false" />,
						<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#next.getIdPermission()#" null="false" />
						)
					</cfquery>
			
				</cfloop>
				
				<cfcatch type="any">
				
					<cftransaction action="rollback" />
					
					<cfthrow message="#cfcatch.Message#" detail="#cfcatch.Detail#" />
				
				</cfcatch>
			
			</cftry>
			
		</cftransaction>
			
	</cffunction>
	
	<cffunction name="getPermissions" access="public" returntype="Query" output="false">
		<cfargument name="idRole" type="String" required="true" />
		
		<cfset var qry="" />

		<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
			SELECT		Permission.name, Permission.description, Permission.idPermission, 
						Permission.id, Role_Permission.idRole
			FROM		Role_Permission INNER JOIN
                      	Permission ON Role_Permission.idPermission = Permission.idPermission
			WHERE		Role_Permission.idRole IN( #arguments.idRole# )
		</cfquery>
		
		<cfreturn qry/>
	</cffunction>
	
	<cffunction name="getPreferences" access="public" returntype="Query" output="false">
		<cfargument name="idRole" type="String" required="true" />
		
		<cfset var qry="" />

		<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
			SELECT     Preference.name, Preference.description, Preference.id, Preference.idPreference, Role_Preference.idRole
			FROM       Role_Preference INNER JOIN
                       Preference ON Role_Preference.IdPreference = Preference.idPreference
			WHERE	   Role_Preference.idRole IN( #arguments.idRole# )
		</cfquery>
		
		<cfreturn qry/>
	</cffunction>

</cfcomponent>
